package controller;

import dao.Album;
import dao.Artist;
import model.AlbumModel;
import model.ArtistModel;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ArtistServlet extends HttpServlet {
    private static final String ARTIST_ID = "artistId";
    private static final String ARTIST_ATTRIBUTE = "artist";
    private static final String ALBUMS_ATTRIBUTE = "albums";
    private static final String ARTIST_VIEW = "view/artist.jsp";

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String artistId = request.getParameter(ARTIST_ID);
        ArtistModel artistModel = ArtistModel.getInstance();

        Artist artist = artistModel.getArtistById(artistId);
        request.setAttribute(ARTIST_ATTRIBUTE, artist);

        AlbumModel albumModel = AlbumModel.getInstance();
        List<Album> albums = albumModel.getAlbumsByArtistId(artistId);
        request.setAttribute(ALBUMS_ATTRIBUTE, albums);

        RequestDispatcher view = request.getRequestDispatcher(ARTIST_VIEW);
        view.forward(request, response);
    }
}
